﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Net.Http;
using System.Threading.Tasks;
using Lhn.Ds.Core.CarDevice;
using Lhn.Ds.Core.Exceptions;

namespace Lhn.Ds.Core.Service
{
    [Export(typeof(UserService))]
    public class UserService : BaseService
    {
        public async Task<string> Login(string user, string password)
        {
            try
            {
                var rt = await Client.Post<string, FormUrlEncodedContent>("/web/login", new FormUrlEncodedContent(
                    new[]
                    {
                        new KeyValuePair<string, string>("username", user),
                        new KeyValuePair<string, string>("password", password)
                    }
                ));
                return rt;
            }
            catch (ResposeFailException e)
            {
                throw new LoginException(e);
            }
        }
    }
}